<?php  
$path = drupal_get_path('module', 'website_directory'); 
drupal_add_css($path . '/css/email_directory.css');
drupal_add_js($path . '/js/website_directory.js');

function Emaillinks(){ 
global $base_url;
global $language;
 if($language->language=='ta'){
	$lang= 'ta';
 }else{
 	$lang='';
 }
	$url = $base_url . '/' . drupal_get_path('module', 'website_directory');
	$output .="<div class ='dir_left_corner'>&nbsp;</div>
                  <div class ='dir_head_content'>
					<span class = 'header_desi1'>".t('Email Directory')."</span>
				</div>
				<div class ='dir_right_corner'>&nbsp;</div><div class='clr'></div>";
	$output .='<div class="dir_list"> 
	<div class="dir_content"> <img src="'.$url.'/images/icon_main.png"> '.l(t('Chief Minister\'s Special Cell'),'emaildirectory/cmcell').'</div>
	<div class="dir_content"> <img src="'.$url.'/images/icon_main.png"> '.l(t('District Collectorates'),'emaildirectory/distcollectors').'</div>	
	<div class="dir_content"> <img src="'.$url.'/images/icon_main.png"> '.l(t('Secretaries to Government of '.SITE_STATE.''),'emaildirectory/sectn').'</div>
	<div class="dir_content"> <img src="'.$url.'/images/icon_main.png"> '.l(t('Nodal Officers in Secretariat'),'emaildirectory/nodalsec').'</div>
	<div class="dir_end"></div></div>';
 	$output .= "<div class='txtdirectory' ><h4>".l(t('Contact Directory'),'contact_directory')."</h4></div>";
	return $output;
}

function email_directory_allinks($emailType){
$emailType =arg(1);
switch($emailType) {
		case "cmcell":
				$EmailData = cmaddress();
				break;
		case "sectn":
				$EmailData = viewsecretaries();
				break;	
		case "distcollectors":
				$EmailData = viewcollectors();
				break;
		case "nodalsec":
				$EmailData = viewnodalofficers();
				break;		
	}
	return $EmailData;
}
function cmaddress() {
global $base_url;
$html .= "<div class='head_title' >Cheif Minister's Email Address</div>";
$html .="<div class ='dir_left_corner'>&nbsp;</div>
                  <div class ='dir_head_content'>
					<span class = 'header_desi' style='width:55%;'>".t('Designation')."</span>
					<span class = 'header_direct'> ".t('Email')."</span>
				</div>
				<div class ='dir_right_corner'>&nbsp;</div>";
			my_db_change(1); 	
 $result = db_query("select distinct * from  field_data_field_cont_org_name ,field_data_field_cont_desg,field_data_field_cont_email
 where field_data_field_cont_desg.entity_id=field_data_field_cont_org_name.entity_id and 
 field_data_field_cont_desg.revision_id=field_data_field_cont_org_name.revision_id and
 field_data_field_cont_desg.entity_id=field_data_field_cont_email.entity_id and 
 field_data_field_cont_desg.revision_id=field_data_field_cont_email.revision_id and  
 cast(field_cont_org_name_value as int) in (select nid from node where title like '%Chief Minister''s Special Cell%')");
 my_db_change(0); 
	foreach($result as $contentlist){	
	        $results .= '<div class = "dir_list">';
			$results .=  '<div class = "dir_desi" >'.$contentlist->field_cont_desg_value.'</div>';
			$results .=  '<div class = "dir_phone" >' .Email_Replace($contentlist->field_cont_email_value).'</div>';
			$results .=  '<div class="clr"></div>';
			$results .= '</div>';
	}
	
	$html .= '<div class = "clr"></div>';
	$html .= $results;
	$html .= '<div class="dir_end"></div>';
return $html;
}
function viewsecretaries(){
global $base_url;
drupal_set_title("E-Mail addresses of Secretaries to Government of ".SITE_STATE."");
	$html = "<div><div style='text-align:right;'>".printPage()."</div><div class='clr'></div></div>";	
	$html .="<div class ='dir_left_corner'>&nbsp;</div>
					  <div class ='dir_head_content'>
					 <span class = 'header_desi' style='width:75%;'>".t('Designation')."</span>
						<span class = 'header_direct'> ".t('Email')."</span>
					</div>
					<div class ='dir_right_corner'>&nbsp;</div>";
	my_db_change(1);
	$result=db_query("select  distinct em.entity_id,dg.entity_id,field_cont_desg_value,field_cont_email_value 
 from field_data_field_cont_desg as dg LEFT join field_data_field_cont_email as em on
 dg.entity_id =em.entity_id  where field_cont_desg_value like '%Secretary%' or field_cont_desg_value like '%Commissioner%' or field_cont_desg_value like '%Electrol%'");
 my_db_change(0); 	
foreach($result as $contentlist){
	        $results .= '<div class = "dir_list_email">';
			$results .=  '<div class = "dir_desi" >'.$contentlist->field_cont_desg_value.'</div><div style="padding:5px;"></div>';
			$results .=  '<div class = "dir_phone" >' .Email_Replace($contentlist->field_cont_email_value).'</div>';
			$results .=  '<div class="clr"></div>';
			$results .=  '<div ></div>';			
			$results .= '</div>';	
	}
	
	$html .= '<div class = "clr"></div>';
	$html .= $results;
	$html .= '<div class="dir_end"></div>';
 				
return $html;
}
function viewcollectors() {
global $base_url,$language;
drupal_set_title("E-Mail addresses of District Collectors");
$html = "<div><div style='text-align:right;'>".printPage()."</div><div class='clr'></div></div>";	
	

$html .="<div class ='dir_left_corner'>&nbsp;</div>
                  <div class ='dir_head_content'>
				 <span class = 'header_desi' style='width:35%;'>".t('District')."</span>
				 <span class = 'header_desi' style='width:40%;'>".t('Name')."</span>
					<span class = 'header_direct'> ".t('Email')."</span>
				</div>
				<div class ='dir_right_corner'>&nbsp;</div>";
				my_db_change(1); 	
 $result = db_query("select distinct dt.dt_name,dt.dt_cd,n.nid,n.title,em.field_cont_email_value from node as n Join field_data_field_cont_destination as dest on  n.nid=dest.entity_id and 
 cast(dest.field_cont_destination_value as text) = '42'  join field_data_field_cont_email as em on n.nid=em.entity_id  
 join field_data_field_cont_dist as f on n.nid= f.entity_id join district_mst as dt on cast(dt.dt_cd as text)= f.field_cont_dist_value where (n.language='".$language->language."' OR n.language = '')");
		my_db_change(0); 	
	foreach($result as $contentlist){
	       $results .= '<div class = "dir_list_email">';
			$results .=  '<div class = "dir_desi" style="width:30%;">'.t($contentlist->dt_name).'</div><div style="padding:5px;"></div>';
			$results .=  '<div class = "dir_phone" style="width:35%;" >' .$contentlist->title.'</div>';
			$results .=  '<div class = "dir_phone" >' .Email_Replace($contentlist->field_cont_email_value).'</div>';
			$results .=  '<div class="clr"></div>';
			$results .= '</div>';
	}
	
	$html .= '<div class = "clr"></div>';
	$html .= $results;
	$html .= '<div class="dir_end"></div>';
return $html;
}
function viewnodalofficers() {
global $base_url;
drupal_set_title("Email addresses of Nodal Officers for Computerisation in Secretariat");
$html = "<div><div style='text-align:right;'>".printPage()."</div><div class='clr'></div></div>";	

$html .="<div class ='dir_left_corner'>&nbsp;</div>
                  <div class ='dir_head_content'>
				    <span class = 'header_desi' style='width:65%;'>".t('Name of the Department / Office')."</span>				
					<span class = 'header_direct'> ".t('Email')."</span>
				</div>
				<div class ='dir_right_corner'>&nbsp;</div>";
	 my_db_change(1); 
 	 $result = db_query("SELECT u.uid, u.mail, d.dept_name 
			FROM users AS u, profile_values AS p, depart_mst AS d 
			WHERE u.uid IN(SELECT uid FROM users_roles WHERE rid=4) 
			AND (p.fid=9 AND u.uid=p.uid) AND (d.dep_id::text = p.value) 
			ORDER BY d.dept_name ASC");
	
	my_db_change(0); 
	$results = '';
		foreach($result as $links){
		$results .= '<div class = "dir_list_email">';
		$results .=  '<div class = "dir_desi" style="width:50%;">'.$links->dept_name.'</div><div style="padding:5px;"></div>';
		$results .=  '<div class = "dir_phone" style="width:30%;" >' .Email_Replace($links->mail).'</div>';
		$results .=  '<div class="clr"></div>';
		$results .= '</div>';   		
	}
	
	$html .= '<div class = "clr"></div>';
	$html .= $results;
	$html .= '<div class="dir_end"></div>';

return $html;
}
//print option 
function printPage(){
global $base_url;
$output = '<div><div style="text-align:right;"><div style="text-align:right;"><a href="javascript:window.print()"><img title="Print" alt="Print" style="border:0px;" src="'.$base_url.'/sites/default/files/print.gif"></a></div></div><div class="clr"></div></div>';
return $output;
}